package com.ruoyi.coal.modules.coalpile.utils.schedule;

import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import javax.annotation.PostConstruct;

import com.ruoyi.coal.modules.coal.service.StartRadarService;
import com.ruoyi.coal.modules.coalpile.domain.TimingEntity;
import com.ruoyi.coal.modules.coalpile.service.CoalYardInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Timed {
    private static final Logger log = LoggerFactory.getLogger(com.ruoyi.coal.modules.coalpile.utils.schedule.Timed.class);

    private static StartRadarService startRadarServiceStatic;

    private static CoalYardInfoService coalYardInfoServiceStatic;

    @Autowired
    StartRadarService startRadarService;

    @Autowired
    CoalYardInfoService coalYardInfoService;

    @PostConstruct
    public void initialize() {
        startRadarServiceStatic = this.startRadarService;
        coalYardInfoServiceStatic = this.coalYardInfoService;
    }

    public static void deleteSchedule(Integer id) {
        CronUtil.remove(id.toString());
        System.out.println("删除定时任务"+ id);
    }

    public static void setMonitor(TimingEntity timingEntity) {
        System.out.println(timingEntity.getId() + "定时任务启动");
                CronUtil.schedule(timingEntity.getId().toString(), timingEntity.getCronExpression(), (Task)timingEntity);
    }

    public static Long dateToStamp(String s) {
        s = LocalDate.now() + " " + s;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Long.valueOf(date.getTime());
    }
}
